var info = InitialParameter("/json/Scenario.json")

/**
 * 解析Scenario.json文件
 * @returns 
 */
function TransformScenarioData(trackNameIndex) {
    var thisResult = {}
    var CameraResult = [],
        ActivityResult = [],
        TrackNames = []
    var paths;

    // var cameraTrackInfo = info.Scenario.Tracks.CameraTrack.Activaties
    // var ActivatiesTrackInfo = info.Scenario.Tracks.Activaties
    var cameraTrackInfo = []
    var ActivatiesTrackInfo = []
    var partTrack = info.Scenario.Tracks.PartTrack
        //获取某分段数据信息
    partTrack.map(function(onePart) {
        TrackNames.push(onePart.TrackName)
        if (onePart.TrackName == trackNameIndex) {
            //判断是否存在对应Part,如果不存在，直接解析onePart,存在的话则根据partName索引解析对应的Part内容
            if (!onePart.PartName && onePart.SceneTrack && onePart.CameraTrack && onePart.Activaties) {
                cameraTrackInfo = onePart.CameraTrack.Activaties
                ActivatiesTrackInfo = onePart.Activaties
            } else {
                var partName = onePart.PartName
                var partContent = info.Scenario.Parts[partName]
                if (partContent) {
                    cameraTrackInfo = partContent.CameraTrack.Activaties
                    ActivatiesTrackInfo = partContent.Activaties
                }
            }
        }
    })
    if (cameraTrackInfo.length > 0) {
        //相机飞行信息
        cameraTrackInfo.map(function(oneActivity) {
            for (var i in oneActivity) {
                var obj = {}
                switch (oneActivity[i].Actor) {
                    case "CameraActor":
                        var camerasIndex = oneActivity[i].Parameters.Camera
                        var cameraInfo = info.Scenario.Cameras[camerasIndex]
                            // obj = $.extend(cameraInfo,oneActivity[i].Parameters)
                        obj = {
                            "Heading": cameraInfo.Heading,
                            "Pitch": cameraInfo.Pitch,
                            "Roll": cameraInfo.Roll,
                            "X": cameraInfo.X,
                            "Y": cameraInfo.Y,
                            "Z": cameraInfo.Z,
                            "Camera": oneActivity[i].Parameters.Camera,
                            "Duration": oneActivity[i].Parameters.Duration,
                            "Time": oneActivity[i].Parameters.Time,
                            //  "AppointCamera": i
                        }
                        break;
                    case "PathActor":
                        obj = oneActivity[i].Parameters;
                        paths = oneActivity[i].Parameters.Path;
                        if (!paths[0].Z) {
                            var Newpaths = [];
                            if (false) { //TODO 增加判断
                                var allPoints = [];
                                for (var p = 0; p < paths.length; p++) {
                                    allPoints.push(Cesium.Cartographic.fromDegrees(paths[p][0], paths[p][1]));
                                }
                                if (viewer.terrainProvider) {
                                    new Promise(async resolve => {
                                        try {
                                            var promise = Cesium.sampleTerrain(viewer.terrainProvider, 15, allPoints);
                                            Cesium.when(promise, (updatedPositions) => {
                                                for (var k = 0; k < updatedPositions.length; k++) {
                                                    Newpaths[k] = {};
                                                    Newpaths[k].X = paths[k][0];
                                                    Newpaths[k].Y = paths[k][1];
                                                    Newpaths[k].Z = updatedPositions[k].height + info.Scenario.Global.CameraHeight;
                                                }
                                                resolve(paths)
                                            })
                                        } catch (e) {
                                            resolve(false)
                                        }
                                    })
                                } else {
                                    for (var k = 0; k < paths.length; k++) {
                                        Newpaths[k] = {
                                            "X": paths[k][0],
                                            "Y": paths[k][1],
                                            "Z": updatedPositions[k].height + info.Scenario.Global.CameraHeight
                                        }
                                    }
                                }
                            } else {
                                for (var k = 0; k < paths.length; k++) {
                                    Newpaths[k] = {
                                        "X": paths[k][0],
                                        "Y": paths[k][1],
                                        "Z": info.Scenario.Global.CameraHeight
                                    }
                                }
                            }
                            //计算两点之间的距离，并重新分配时间
                            var num = obj.Duration / getDistance(Newpaths);
                            for (let i = 0; i < Newpaths.length; i++) {
                                if (i == 0) {
                                    Newpaths[i].FlyTime = info.Scenario.Global.FlyDuration;
                                } else if (Newpaths[i - 1]) {
                                    Newpaths[i].FlyTime = Math.round(num * getGeodesicDistance(Newpaths[i - 1], Newpaths[i]));
                                }
                            }
                            oneActivity[i].Parameters.Path = Newpaths;
                        }

                        break;
                    default:
                        break;
                }
                obj["Actor"] = oneActivity[i].Actor;
                obj["Action"] = oneActivity[i].Action;
                obj["Activaties"] = [];
                obj["AppointCamera"] = i;
                CameraResult.push(obj);
            }
        })
    }
    if (ActivatiesTrackInfo.length > 0) {
        //实体与时序信息
        ActivatiesTrackInfo.map(function(oneActivity) {
            for (var i in oneActivity) {
                var obj = {}
                switch (oneActivity[i].Actor) {
                    case "EntityActor":
                        var entytiesArr = []
                        for (var index = 0; index < oneActivity[i].Parameters.Entities.length; index++) {
                            var entityIndex = oneActivity[i].Parameters.Entities[index]
                            var entityInfo = info.Scenario.Geometries[entityIndex]
                            entityInfo.Symbol["entityName"] = entityIndex
                            oneActivity[i].Actions.map(function(oneAction) {
                                for (var key in oneAction) {
                                    entityInfo[key] = oneAction[key]
                                }
                            })
                            entytiesArr.push(entityInfo)
                        }
                        // oneActivity[i].Parameters.Entities = entytiesArr
                        CameraResult = CameraResult.map(function(item) {
                            if (oneActivity[i].Parameters.AppointCamera == item.AppointCamera) {
                                item["Activaties"].push({
                                    "Actor": oneActivity[i].Actor,
                                    "Sequential": oneActivity[i].Parameters.Sequential,
                                    "Entities": entytiesArr
                                })
                            }
                            return item
                        })
                        break;
                    case "LayerActor":

                        break;
                    case "CustomActor":
                        CameraResult = CameraResult.map(function(item) {
                            if (oneActivity[i].Parameters.AppointCamera == item.AppointCamera) {
                                oneActivity[i].Actions.map(function(oneAction) {
                                    for (var key in oneAction) {
                                        oneActivity[i].Parameters[key] = oneAction[key]
                                    }
                                })
                                oneActivity[i].Parameters["containerIndex"] = i
                                oneActivity[i].Parameters["Time"] = item["Time"];
                                item["Activaties"].push({
                                    "Actor": oneActivity[i].Actor,
                                    "Entities": oneActivity[i].Parameters
                                })
                            }
                            return item
                        })
                        break;
                    default:
                        break;
                }
            }
        })
    }

    thisResult = {
        "TrackNames": TrackNames,
        "CameraTrack": CameraResult,
        "Activaties": ActivityResult
    }
    return thisResult
}

//读取Json文件
function InitialParameter(url) {
    var info = null;
    $.ajax({
        async: false,
        url: url,
        success: function(result) {
            info = result;
        },
        error: function(result) {
            console.log(result);
        }
    });
    return info;
}